package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.HelpAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.HelpBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_platform)
    TextView tvPlatform;
    @BindView(R.id.tv_trade)
    TextView tvTrade;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private HelpAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.HelpList, HelpActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        HelpBean parentBean = DealResult.getInstance().dealDatas(HelpActivity.this, response, new TypeToken<HelpBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        } else {
                            ToastUtils.toastForShort(HelpActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(HelpActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<HelpBean.BodyBean> body) {
        mAdapter.addAll(body);
        recyclerView.refreshComplete(mAdapter.getItemCount());
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("在线帮助");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new HelpAdapter(this);
        mAdapter.setDataList(new ArrayList<HelpBean.BodyBean>());
        LRecyclerViewAdapter lRecyclerViewAdapter=new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(HelpActivity.this,ExplainActivity.class);
                i.putExtra("url",mAdapter.getDataList().get(position).getHelpurl());
                i.putExtra("name",mAdapter.getDataList().get(position).getHelptitle());
                startActivity(i);
            }
        });
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_platform, R.id.tv_trade, R.id.tv_register, R.id.tv_rule, R.id.tv_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_platform:
                startActivity(new Intent(this, ExplainActivity.class));
                break;
            case R.id.tv_trade:
                startActivity(new Intent(this, ExplainActivity.class));
                break;
            case R.id.tv_register:
                startActivity(new Intent(this, ExplainActivity.class));
                break;
            case R.id.tv_rule:
                startActivity(new Intent(this, ExplainActivity.class));
                break;
            case R.id.tv_withdraw:
                startActivity(new Intent(this, ExplainActivity.class));
                break;
        }
    }
}
